<?php

namespace app\index\controller;

use app\common\exception\System as SystemException;
use app\common\exception\User as UserException;

/**
 * 分销
 * Class Distribution
 * @package app\index\controller
 */
class Distribution extends Common
{
    /**
     * 分销中心
     * @return \think\Response
     */
    public function index()
    {
        if (!$this->auth->member_id) {
            return $this->errorReturn(UserException::E_NOT_LOGIN, UserException::$messageList[UserException::E_NOT_LOGIN]);
        }

        // 找出当前用户下面的三级用户
        $users = service('index/Distribution')->getOfflineThirdLevelUsers($this->auth->member_id);

        $res = [
            'one' => count($users['one']),
            'two' => count($users['two']),
            'three' => count($users['three']),
            'reward' => 0, // 奖励
            'commission' => 0, // 佣金
            'dividend' => 0, // 分红
        ];

        return $this->successReturn($res, SystemException::E_SUCCESS, '获取成功');
    }

    /**
     * 获取下级用户列表
     * @return \think\Response
     */
    public function getSubordinatePersonnel()
    {
        if (!$this->auth->member_id) {
            return $this->errorReturn(UserException::E_NOT_LOGIN, UserException::$messageList[UserException::E_NOT_LOGIN]);
        }

        $level = $this->request->get('level', 1);
        $page = $this->request->get('page', 1);
        $limit = 10;

        if (!in_array($level, [1, 2, 3])) {
            return $this->errorReturn(UserException::E_LEVEL_ERROR, UserException::$messageList[UserException::E_LEVEL_ERROR]);
        }

        $users = service('index/Distribution')->getSubordinatePersonnel($this->auth->member_id, $level, $page, $limit);

//        $users = [11];
        $list = service('index/Distribution')->getUserInfo($users);

        return $this->successReturn($list, SystemException::E_SUCCESS, '获取成功');
    }
}